const MergeFunc = function(mfunc){
	this.merge = mfunc;
};
function  defaultMergeFuncSimple(sourceObj,destObj){
	if(!sourceObj){
		return destObj;
	}
	if(!destObj){
		return sourceObj;
	}
	return destObj;
};
function deepMergeFunction(sourceObj,destObj){
	if(!sourceObj){
		return destObj;
	}
	if(!destObj){
		return sourceObj;
	}
	if(typeof(sourceObj) !="object"){
		return destObj;
	}
	if(typeof(destObj) !="object"){
		return destObj;
	}
	let ret = {};
	for(let key in sourceObj){
		ret[key] = null;
	}
	for(let key in destObj){
		ret[key] = null;
	}
	for(let key in ret){
		ret[key] = deepMergeFunction(sourceObj[key],destObj[key]);
	}
	return ret;
}
const deepMergeFunc = new MergeFunc(deepMergeFunction);
const simpleMergeFunc = new MergeFunc(defaultMergeFuncSimple);
const mergeFuncWare = {
	"simpleFunc":simpleMergeFunc,
	"deepFunc":deepMergeFunc
};
let mergeFuncFactory = {
	getMergeFunc:function( functoryName){
		if(mergeFuncWare[functoryName]){
			return mergeFuncWare[functoryName];
		}
		return null;
	}
};


/**
merge 两个对象  合并  不同保留，相同取第二个参数值，深复制
例如 
merge({a:3,b:4},{b:1,c:5}) => {a:3,b:1,c:5}
**/
function merge(source,dest,mergeFunc){
	let ret ={};
    if(!source){
		source ={};
	}
	if(!dest){
		dest = {};
	}
	if(!mergeFunc){
		mergeFunc = mergeFuncFactory.getMergeFunc("simpleFunc");
	}
	for(let key in source){
		ret[key] = null;
	}
	for(let key in dest){
		ret[key] = null;
	}
	for(let key in ret){
		ret[key] = mergeFunc.merge(source[key],dest[key]);
	}
	return ret;
}

function joinString(baseStr,filterStr){
    // 数组
    if(Array.isArray(baseStr)){
        let ret="";
        if(baseStr.length){
            ret += baseStr[0];
            for(var bix =1;bix <baseStr.length;bix++ ){
                ret += filterStr + baseStr[bix];
            }
        }
        return ret;
    }else{
        // 其他
        return baseStr;
    }
}
module.exports = {
    "merge":merge,
    "joinString":joinString,
    "mergeFuncFactory":mergeFuncFactory,
    "MergeFunc":MergeFunc
};